#pragma once

#include<easyx.h>

#include"definition.h"
#include"snake.h"
#include"fruit.h"

extern Head snakehead;
extern Body snakebody;
extern Fruit fruit;
extern IMAGE Apple;
extern IMAGE Speeding;

class GameScene
{
public:
	GameScene()= default;
	~GameScene() = default;

	void draw_game_scene()
	{
		//gamescenebackground
		setlinecolor(RGB(100, 100, 100));
		for (int i = GameSceneBoundaryLeft; i <= GameSceneBoundaryRight; i += GridSize)
		{
			line(i, GameSceneBoundaryTop, i, GameSceneBoundaryBottom);
		}
		for (int i = GameSceneBoundaryTop; i <= GameSceneBoundaryBottom; i += GridSize)
		{
			line(GameSceneBoundaryLeft, i, GameSceneBoundaryRight, i);
		}

		//head
		setfillcolor(RGB(150, 0, 0));
		setlinecolor(RGB(150, 0, 0));
		int HeadPosX_Gamescene = GameSceneBoundaryLeft + (snakehead.GetPosX() - 1) * GridSize;
		int HeadPosY_Gamescene = GameSceneBoundaryTop + (snakehead.GetPosY() - 1) * GridSize;
		fillrectangle(HeadPosX_Gamescene, HeadPosY_Gamescene, HeadPosX_Gamescene + GridSize, HeadPosY_Gamescene + GridSize);

		//body
		// 修改原蛇身绘制部分
int bodyLength = snakebody.GetBodyLength();
for (int i = 0; i < bodyLength; i++) 
{
    // 先计算当前身体颜色
    float hue = (int)((float)20*i / BodyMaxLength * 360)%360;
    COLORREF bodyColor = HSLtoRGB(hue, 0.8f, 0.6f);

    // 获取当前身体部分坐标
    int currentX = GameSceneBoundaryLeft + (snakebody[i].GetPosX() - 1) * GridSize;
    int currentY = GameSceneBoundaryTop + (snakebody[i].GetPosY() - 1) * GridSize;

    // 获取历史位置
    auto history = snakebody[i].GetPositionHistory();

    // 绘制拖尾
    for (size_t j = 0; j < history.size()&&is_speeding; j++) {
        float alpha = 1.0f - (float)j / BodyCell::MAX_HISTORY;

        int hx = GameSceneBoundaryLeft + (history[j].x - 1) * GridSize;
        int hy = GameSceneBoundaryTop + (history[j].y - 1) * GridSize;

        COLORREF tailColor = RGB(
            GetRValue(bodyColor) * alpha,
            GetGValue(bodyColor) * alpha,
            GetBValue(bodyColor) * alpha
        );

        setfillcolor(tailColor);
        setlinecolor(tailColor);  // 同步边框颜色

        fillrectangle(hx, hy, hx + GridSize, hy + GridSize);
    }

    // 绘制当前身体部分
    setfillcolor(bodyColor);
    setlinecolor(bodyColor);
    fillrectangle(currentX, currentY, currentX + GridSize, currentY + GridSize);
}

		//fruit

		if (fruit.isBlinking) {
			// ��˸������ÿ֡������ʾ/����
			if ((fruit.blinkTimer / 3) % 2 == 0)
			{
				putimage_alpha(GameSceneBoundaryLeft + (fruit.GetPosX() - 1) * GridSize + 2, GameSceneBoundaryTop + (fruit.GetPosY() - 1) * GridSize + 2, &Apple);  // ����ƻ��
			}
			fruit.blinkTimer++;

			// 30֡��ֹͣ��˸
			if (fruit.blinkTimer > 12) {
				fruit.isBlinking = false;
			}
		}
		else
		{
			putimage_alpha(GameSceneBoundaryLeft + (fruit.GetPosX() - 1) * GridSize + 2, GameSceneBoundaryTop + (fruit.GetPosY() - 1) * GridSize + 2, &Apple);  // ��������
		}
		// ��ʳ����ƺ����Ӳ��ƶ���
		if (fruit.isRippling) {
			setlinecolor(RGB(255, 255, 255));
			circle(
				GameSceneBoundaryLeft + (fruit.GetPosX() - 1) * GridSize + GridSize / 2,
				GameSceneBoundaryTop + (fruit.GetPosY() - 1) * GridSize + GridSize / 2,
				fruit.rippleRadius
			);
			fruit.rippleRadius += 2;

			// 30֡��ֹͣ����
			if (fruit.rippleRadius > 30) {
				fruit.isRippling = false;
			}
		}

		//CentText
		static TCHAR CentText[100];
		_stprintf_s(CentText, _T("Score:%d"), cent);
		settextcolor(RGB(150, 150, 150));
		outtextxy(WindowWidth / 2 - 130, (WindowHeight - GameSceneSizeVirtiacal_Bit) / 4, CentText);

		//HighestScore
		static TCHAR HighestScore[20];
		if (cent < highest_score)
		{
			_stprintf_s(HighestScore, _T("Highest Score:%d"), highest_score);
		}
		else
		{
			_stprintf_s(HighestScore, _T("Highest Score:%d"), cent);
		}
		settextcolor(RGB(150, 150, 150));
		outtextxy(WindowWidth / 2-30, (WindowHeight - GameSceneSizeVirtiacal_Bit) / 4, HighestScore);

		//speeding
		if (is_speeding)
		{
			putimage_alpha(WindowWidth / 2 + 200, (WindowHeight - GameSceneSizeVirtiacal_Bit) / 4 - 2, &Speeding);
		}
	}
private:
};